(function($, window, undefined) {
	"use strict";
	var simapp = function() {};

	// 设置与获取CSS
	simapp.prototype.css = function(obj, attr, value) {
		switch (arguments.length) {
			case 2:
				if (typeof arguments[1] === "string") {
					return getComputedStyle(obj, false)[attr];
				} else {
					for (var i in arguments[1]) {
						obj.style[i] = attr[i];
					}
				}
				break;
			case 3:
				obj.style[attr] = value;
				break;
			default:
				break;
		};
	};

	// 显示提示信息
	simapp.prototype.toast = function(message) {
		var self = this;
		var container = document.createElement("div");
		var inner = document.createElement("span");
		inner.innerHTML = message;
		this.css(container, {
			position: "fixed",
			top: "50%",
			left: "50%",
			width: "100%",
			maxWidth: "90%",
			WebkitTransform: "translate(-50%,-50%)",
			msTransform: "translate(-50%,-50%)",
			transform: "translate(-50%,-50%)",
			zIndex: 9999,
			transition: "opacity .5s ease-in-out",
			WebkitTransition: "opacity .5s ease-in-out",
			MsTransition: "opacity .5s ease-in-out",
			wordBreak: "break-word",
			textAlign: "center"
		});
		this.css(inner, {
			display: "inline-block",
			padding: ".8em 1em",
			borderRadius: ".3em",
			backgroundColor: "rgba(51,51,51,.8)",
			color: "#fff",
			textAlign: "left"
		});
		container.appendChild(inner);
		document.body.appendChild(container);
		setTimeout(function() {
			self.css(container, {
				opacity: 0
			});
			setTimeout(function() {
				document.body.removeChild(container)
			}, 500);
		}, 2000);
	};

	simapp.prototype.tips = function(message) {
		var simTipsContainer = $("<div></div>").css({
			position: 'fixed',
			top: 0,
			left: 0,
			width: '100%',
			zIndex: 998,
		});
		var simTipsContent = $("<div></div>").html(message).css({
			position: "relative",
			zIndex: 998,
			margin: "0 10px",
			padding: "10px 40px 10px 10px",
			backgroundColor: "#fff",
			borderRadius: "0 0 5px 5px",
		});
		var simTipsArrow = $("<div></div>").css({
			position: "absolute",
			top: 0,
			right: 20,
			width: 30,
			height: 30,
			zIndex: 999,
			background: "url('') no-repeat right top",
			backgroundSize: "30px 30px",
		});
		var simTipsMask = $("<div></div>").css({
			position: "fixed",
			top: 0,
			left: 0,
			width: '100%',
			height: '100%',
			backgroundColor: "rgba(36, 39, 42, .8)",
		}).click(function() {
			simTipsContainer.remove();
		});
		simTipsContainer.append(simTipsContent).append(simTipsArrow).append(simTipsMask).appendTo('body');
	}

	// 动作模块
	simapp.prototype.actions = function(config) {
		var oAction = document.createElement("div");
		oAction.setAttribute("class", "actions-modal");
		var html = "",
			_this = this;
		for (var i = 0; i < config.length; i++) {
			for (var j = 0; j < config[i].length; j++) {
				0 === j && (html += "<div class='actions-modal-group'>")
				var h = config[i][j],
					itemClass = (h.label ? "actions-modal-label" : "actions-modal-button");
				html += "<span class='" + itemClass + "'>" + h.text + "</span>";
				j === config[i].length - 1 && (html += "</div>");
			}
		}

		oAction.innerHTML = html;
		document.body.appendChild(oAction);
		var oActionHeight = getComputedStyle(oAction, null)["height"];
		oAction.style.marginTop = oActionHeight;

		var modalGroups = oAction.querySelectorAll(".actions-modal-group");
		[].forEach.call(modalGroups, function(itemGroups, group) {
			var itemGroup = itemGroups.children;
			[].forEach.call(itemGroup, function(itemButton, button) {
				if (itemButton.classList.contains("actions-modal-button")) {
					config[group][button].click && itemButton.addEventListener("touchend", config[group][button].click, false);
					config[group][button].close && itemButton.addEventListener("touchend", function() {
						_this.closeModal(oAction);
					}, false);
				}
			});
		});
		this.openModal(oAction);
	};

	// 弹出模块
	simapp.prototype.alert = function(configs) {
		var defaults = {
			"text": "",
			"title": "提示",
			"buttons": [{
				"text": "确认",
				"close": true,
				"click": configs.callback
			}]
		}
		for (var i in defaults) {
			configs[i] || (configs[i] = defaults[i]);
		};
		return this.modalAll(configs);
	};

	// 确认模块
	simapp.prototype.confirm = function(configs) {
		var defaults = {
			"text": "",
			"title": "提示",
			"buttons": [{
				"text": "取消",
				"close": true
			}, {
				"text": "确认",
				"close": true,
				"click": configs.callback
			}]
		}
		for (var i in defaults) {
			configs[i] || (configs[i] = defaults[i]);
		};
		return this.modalAll(configs);
	};

	// 弹出，确认引用模块
	simapp.prototype.modalAll = function(configs) {
		var oModal = document.createElement("div");
		oModal.setAttribute("class", "modal");
		var buttonsHTML = "";
		var modalTemplate = "<div class='modal-inner'><div class='modal-title'>{{title}}</div><div class='modal-text'>{{text}}</div></div><div class='modal-buttons'>{{buttons}}</div>";
		if (configs.buttons && configs.buttons.length > 0) {
			for (var i = 0; i < configs.buttons.length; i++) {
				buttonsHTML += "<span class='modal-button'>" + configs.buttons[i].text + "</span>";
			}
		}
		var html = modalTemplate.replace(/{{title}}/g, configs.title).replace(/{{text}}/g, configs.text).replace(/{{buttons}}/g, buttonsHTML);
		oModal.innerHTML = html;
		document.body.appendChild(oModal);

		//添加事件
		var modalButtons = oModal.querySelectorAll(".modal-button"),
			_this = this;
		[].forEach.call(modalButtons, function(itemButton, index) {
			configs.buttons[index].click && itemButton.addEventListener("touchend", configs.buttons[index].click, false);
			configs.buttons[index].close && itemButton.addEventListener("touchend", function() {
				_this.closeModal(oModal);
			}, false);
		});

		this.openModal(oModal);
	};

	simapp.prototype.openModal = function(elem) {
		var div = document.createElement("div");
		div.setAttribute("class", "modal-overlay");
		document.body.appendChild(div);

		setTimeout(function() {
			elem.classList.add("modal-in");
			div.classList.add("modal-overlay-visible");
		}, 0);
	};

	simapp.prototype.closeModal = function(elem) {
		elem.classList.remove("modal-in");
		elem.classList.add("modal-out");
		var overlay = document.querySelector(".modal-overlay");
		overlay.classList.contains("modal-overlay-visible") && overlay.classList.remove("modal-overlay-visible");

		setTimeout(function() {
			document.body.removeChild(elem);
			document.body.removeChild(overlay);
			///document.querySelector ( ".modal-overlay" ).remove();
			// 手机上面不支持 remove() 方法。微信浏览器，android 4.0
		}, 500);
	};

	$(function() {
		$("[data-role]").click(function() {
			var dataRole = $(this).attr("data-role");
			switch (dataRole) {
				case 'refresh':
					location.reload();
					break;
				case 'back':
					history.go(-1);
					break;
			}
		});
	})

	window.simapp = new simapp();
})(Zepto, window);